package com.future.mayi.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.TextView;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.adapter.InviteAdapter;
import com.future.mayi.app.App;
import com.future.mayi.model.InviteItemModel;
import com.future.mayi.response.InviteResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;

public class InviteNextActivity extends BaseTitleActivity implements XRecyclerView.LoadingListener {
    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    @BindView(R.id.tv_all_num)
    TextView tvAllNum;
    @BindView(R.id.tv_tuan_num)
    TextView tvTuanNum;

    private InviteAdapter adapter;

    private InviteItemModel model;

    public static void startActivity(Context context, InviteItemModel model) {
        Intent intent = new Intent(context, InviteNextActivity.class);
        intent.putExtra("model", model);
        context.startActivity(intent);
    }

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("间系团长列表");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_invite_next);
    }

    @Override
    protected void initView() {
        adapter = new InviteAdapter(mContext, recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingMoreEnabled(false);
    }

    @Override
    protected void initDates() {
        model = (InviteItemModel) getIntent().getSerializableExtra("model");
    }

    @Override
    protected void initListener() {
        if (model == null) {
            finish();
            return;
        }
        recyclerView.setLoadingListener(this);
        recyclerView.refresh();
    }


    @Override
    public void onRefresh() {
        getData();
    }

    @Override
    public void onLoadMore() {

    }


    private void getData() {
        if (model == null) {
            finish();
            return;
        }
        new HttpRequest<InviteResponse>()
                .with(getActivity())
                .setApiCode(ApiCst.getInviteMy)
                .addParam("user_id", model.getUser_id())
                .addParam("token", App.getToken(mContext))
                .setListener(new HttpRequest.OnNetworkListener<InviteResponse>() {

                    @Override
                    public void onSuccess(InviteResponse response) {
                        if (response.model != null) {
                            tvAllNum.setText(String.valueOf(response.model.getUser().getTotal()));
                            tvTuanNum.setText(String.valueOf(response.model.getUser().getAgent()));
                            HCUtils.refreshListForPage(recyclerView, adapter, response.model.getList(), 0, 20);
                        } else HCUtils.refreshListForPage(recyclerView, adapter, null, 0, 20);
                    }

                    @Override
                    public void onFail(String message, InviteResponse response) {
                        HCUtils.refreshFail(recyclerView, 0, mContext, response);
                    }
                }).start(new InviteResponse());


    }
}
